import 'dart:math';

import 'package:al_pay_flutter/pages/home/home_page.dart';
import 'package:al_pay_flutter/pages/untils/screen.dart';
import 'package:flutter/material.dart';
import '../my/my_page.dart';
import '../evaluate/evaluate_page.dart';
import '../frends/friend_page.dart';
import '../riches/riche_page.dart';

class HomeLayout extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeLayoutState();
  }
}

class _HomeLayoutState extends State<HomeLayout> {
  // PageController _pageController;
  final int number = Random().nextInt(100);
  String tipCount;
  int current = 2;
  List pages = [HomePage(), ReachPage(), EvaluatePage(), FrendPage(), MyPage()];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tipCount = number > 99 ? '99+' : number.toString();
  }

  void tab(v) {
    setState(() {
      // print(v);
      current = v;
    });
  }

  //Stack（层叠布局）+Offstage组合,解决状态被重置的问题
  Widget _getPagesWidget(int index) {
    return Offstage(
      offstage: current != index,
      child: TickerMode(
        enabled: current == index,
        child: pages[index],
      ),
    );
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    ScreenUtil().init(this.context);
    Future.delayed(Duration.zero, () {
      waterMark(this.context);
    });
  }

  @override
  Widget build(BuildContext context) {
    // _pageController = PageController();

    return Scaffold(
      body: Stack(
        children: [
          _getPagesWidget(0),
          _getPagesWidget(1),
          _getPagesWidget(2),
          _getPagesWidget(3),
          _getPagesWidget(4),
        ],
      ),
      // PageView(controller: _pageController, children: pages),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: current,
        iconSize: 24,
        onTap: tab,
        selectedFontSize: 12,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Theme.of(context).primaryColor,
        unselectedItemColor: Color(0xffbbbbbb),
        items: [
          _getBarItem(0xe605, title: '首页'),
          _getBarItem(0xe627, title: '理财'),
          _getBarItem(0xe62f, title: '口碑'),
          _getBarItem(0xe685, title: '消息', tip: tipCount),
          _getBarItem(0xe618, title: '我的'),
        ],
      ),
    );
  }

  waterMark(context) {
    OverlayEntry overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return Container(
          transform: Matrix4.rotationZ(pi/4),
          child: CustomPaint(
            foregroundPainter: WaterMarkPainter(),
          ),
        );
      },
    );
    Overlay.of(context).insert(overlayEntry);
  }

  BottomNavigationBarItem _getBarItem(int codePoint,
      {String title, String tip}) {
    return BottomNavigationBarItem(
        icon: Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Icon(
              IconData(codePoint, fontFamily: 'iconFont'),
              size: 24,
            ),
            tip == null || tip.isEmpty
                ? Text('')
                : Positioned(
                    left: 36.w,
                    top: -4,
                    child: Container(
                      alignment: Alignment.center,
                      width: [32.0.w, 52.0.w, 72.0.w][tip.length - 1],
                      child: Text(
                        tip,
                        style: TextStyle(color: Colors.white),
                      ),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(32.w),
                          color: Colors.red),
                    ),
                  ),
          ],
        ),
        // activeIcon: Icon, // 没有一套的icon
        title: Text(
          title,
          style: TextStyle(
              // color: Color(0xffbbbbbb),
              fontSize: 12),
        ));
  }
}

class WaterMarkPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    for (var i = 0; i < 10; i++) {
      for (var j = 0; j < 10; j++) {
        TextPainter(
            textDirection: TextDirection.ltr,
            text: TextSpan(children: [
              TextSpan(
                  text: Uri.decodeComponent('%E9%A3%98%EF%BC%9A%E7%94%A8%E4%BA%8E%E5%AD%A6%E4%B9%A0%E5%92%8C%E8%A3%85x'),
                  style: TextStyle(
                      fontSize: 16.0, color: Colors.black12, height: 20)),
            ]))
          ..layout(maxWidth: 3750.w, minWidth: 3750.w)
          ..paint(canvas, Offset(700 - 201.0 * i, 500 - 100.0 * j));
      }
    }
  }

  @override
  bool shouldRepaint(WaterMarkPainter oldDelegate) {
    return false;
  }
}
